package com.jf.yytgd.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.InputStream;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description: sql工具类
 * User:22954
 * Date:2025-01-12
 * Time:19:14
 */
@Slf4j
public class SqlUtil {
    private static JdbcTemplate jdbcTemplate;

    public static void init(ApplicationContext context) {
        jdbcTemplate = context.getBean(JdbcTemplate.class);
    }

    public static void executeSqlScript(String sqlScriptPath) {
        String sqlScript;
        try {
            InputStream inputStream = SqlUtil.class.getClassLoader().getResourceAsStream(sqlScriptPath);
            sqlScript = new String(Objects.requireNonNull(inputStream).readAllBytes());
            for (String sql : sqlScript.split(";")) {
                sql = sql.trim();
                if (!sql.isEmpty())
                    jdbcTemplate.execute(sql);
            }
        } catch (Exception e) {
            log.error("execute SqlScript " + sqlScriptPath + " error", e);
        }
    }
}
